package com.zt.rongda.employee.controller;

import com.zt.servlet.*;
import com.zt.rongda.employee.model.EmpTransfer;
import com.zt.rongda.employee.service.EmpTransferService;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;

@Controller
@RequestMapping(value = "/empTransfer")
public class EmpTransferController {

    @Autowired
    private EmpTransferService empTransferService;

    @RequestMapping(value = "/query")
    @ResponseBody
    
    public JSONObject query(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        return Response.response(empTransferService.getTable(reqBean));
    }

    @RequestMapping(value = "/add")
    @ResponseBody
    
    public JSONObject add(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        empTransferService.add(getEmpTransfer(reqBean));
        return Response.responseSuccess();
    }

    @RequestMapping(value = "/update")
    @ResponseBody
    
    public JSONObject update(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        empTransferService.update(getEmpTransfer(reqBean));
        return Response.responseSuccess();
    }

    @RequestMapping(value = "/delete")
    @ResponseBody
    
    public JSONObject delete(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        empTransferService.delete(reqBean.getInfo("listId"));
        return Response.responseSuccess();
    }

    /**
    * 从表单获取对象
    *
    * @param reqBean
    * @return
    */
    private EmpTransfer getEmpTransfer(ReqBean reqBean) {
        EmpTransfer empTransfer = new EmpTransfer();
        empTransfer.setListId(reqBean.getInfo("listId"));
        empTransfer.setEmpId(reqBean.getInfo("empId"));
        empTransfer.setEmpName(reqBean.getInfo("empName"));
        empTransfer.setBefDeptId(reqBean.getInfo("befDeptId"));
        empTransfer.setBefDeptName(reqBean.getInfo("befDeptName"));
        empTransfer.setBefPosId(reqBean.getInfo("befPosId"));
        empTransfer.setBefPosName(reqBean.getInfo("befPosName"));
        empTransfer.setAftDeptId(reqBean.getInfo("aftDeptId"));
        empTransfer.setAftDeptName(reqBean.getInfo("aftDeptName"));
        empTransfer.setAftPosId(reqBean.getInfo("aftPosId"));
        empTransfer.setAftPosName(reqBean.getInfo("aftPosName"));
        empTransfer.setReason(reqBean.getInfo("reason"));
        empTransfer.setStatus(reqBean.getInfo("status"));
        empTransfer.setOperator(reqBean.getUserId());
        empTransfer.setRemark(reqBean.getInfo("remark"));
        return empTransfer;
    }

}